/*************************************************************************
	> File Name: heart_beat.c
	> Author: Maureen 
	> Mail: 1437867946@qq.com 
	> Created Time: Mon 18 Jan 2021 06:50:16 PM CST
 ************************************************************************/

#include "head.h"

extern struct User *users;

void *heart_beat(void *arg) {
    struct Msg heart;
    heart.type = CHAT_HEART;
    while (1) {
        for (int i = 0; i < MAXUSER; i++) {
            if (users[i].flag) { //在线
                if (users[i].cnt <= 0) { //长时间没有回复心跳信息，被心跳线程移除
                    DBG("🔴 "RED"<%s>"NONE" is removed by heart beat...\n", users[i].name);
                    users[i].flag = 0;
                    delete_from_reactor(users[i].fd);
                    DBG("🔱 "RED"%s"NONE" is removed from sub reactor...\n", users[i].name);
                    close(users[i].fd);
                }
                send(users[i].fd, (void *)&heart, sizeof(heart), 0);
                users[i].cnt--;
            }
        }
        sleep(2);
    }
}
